What is @seald-io/binary-search-tree?
@seald-io/binary-search-tree is an npm package that provides a simple and efficient implementation of a binary search tree (BST) data structure. It allows for the storage, retrieval, and manipulation of data in a sorted manner, making it useful for various applications such as searching, sorting, and maintaining a dynamic set of ordered elements.
What are @seald-io/binary-search-tree's main functionalities?
Insertion
This feature allows you to insert key-value pairs into the binary search tree. The keys are used to maintain the order of the elements, and the values are the data associated with those keys.
const { BinarySearchTree } = require('@seald-io/binary-search-tree');
const bst = new BinarySearchTree();
bst.insert(10, 'value1');
bst.insert(5, 'value2');
bst.insert(15, 'value3');
console.log(bst.search(10)); // Outputs: 'value1'
Search
This feature allows you to search for a value associated with a given key in the binary search tree. If the key exists, the corresponding value is returned.
const { BinarySearchTree } = require('@seald-io/binary-search-tree');
const bst = new BinarySearchTree();
bst.insert(10, 'value1');
bst.insert(5, 'value2');
bst.insert(15, 'value3');
console.log(bst.search(5)); // Outputs: 'value2'
Deletion
This feature allows you to delete a key-value pair from the binary search tree. After deletion, the key will no longer exist in the tree.
const { BinarySearchTree } = require('@seald-io/binary-search-tree');
const bst = new BinarySearchTree();
bst.insert(10, 'value1');
bst.insert(5, 'value2');
bst.insert(15, 'value3');
bst.delete(10);
console.log(bst.search(10)); // Outputs: null
Traversal
This feature allows you to traverse the binary search tree in order. The inOrderTraversal method visits all nodes in ascending order of their keys, applying a callback function to each key-value pair.
const { BinarySearchTree } = require('@seald-io/binary-search-tree');
const bst = new BinarySearchTree();
bst.insert(10, 'value1');
bst.insert(5, 'value2');
bst.insert(15, 'value3');
bst.inOrderTraversal((key, value) => {
console.log(key, value);
});
// Outputs:
// 5 'value2'
// 10 'value1'
// 15 'value3'
Other packages similar to @seald-io/binary-search-tree
binary-search-tree
The 'binary-search-tree' package provides a similar implementation of a binary search tree. It supports basic operations like insertion, search, and deletion. However, it may not have as many features or optimizations as @seald-io/binary-search-tree.
avl
The 'avl' package implements an AVL tree, which is a self-balancing binary search tree. It ensures that the tree remains balanced after insertions and deletions, providing better performance for search operations compared to a standard binary search tree.
bintrees
The 'bintrees' package provides implementations for both AVL trees and Red-Black trees. These are self-balancing binary search trees that offer better performance guarantees for dynamic sets of ordered elements. This package is more versatile compared to @seald-io/binary-search-tree.
Binary search trees for Node.js
This module is a fork
of node-binary-search-tree
written by Louis Chatriot for storing indexes
in nedb.
Since the original maintainer doesn't support these packages anymore, we forked
them (here is nedb) and maintain them for the
needs of Seald.
Two implementations of binary search
tree: basic
and AVL (a kind of self-balancing
binmary search tree).
Installation and tests
Package name is @seald-io/binary-search-tree
.
npm install @seald-io/binary-search-tree
If you want to run the tests, you'll have to clone the repository:
git clone https://github.com/seald/node-binary-search-tree
npm install
npm test
Usage
The API mainly provides 3 functions: insert
, search
and delete
. If you do
not create a unique-type binary search tree, you can store multiple pieces of
data for the same key. Doing so with a unique-type BST will result in an error
being thrown. Data is always returned as an array, and you can delete all data
relating to a given key, or just one piece of data.
Values inserted can be anything except undefined
.
const BinarySearchTree = require('binary-search-tree').BinarySearchTree
const AVLTree = require('binary-search-tree').AVLTree
const bst = new BinarySearchTree()
bst.insert(15, 'some data for key 15')
bst.insert(12, 'something else')
bst.insert(18, 'hello')
bst.insert(18, 'world')
bst.search(15)
bst.search(18)
bst.search(1)
bst.betweenBounds({ $lt: 18, $gte: 12 })
bst.delete(15)
bst.delete(18, 'world')
There are three optional parameters you can pass the BST constructor, allowing
you to enforce a key-uniqueness constraint, use a custom function to compare
keys and use a custom function to check whether values are equal. These
parameters are all passed in an object.
Uniqueness
const bst = new BinarySearchTree({ unique: true });
bst.insert(10, 'hello');
bst.insert(10, 'world');
Custom key comparison
const compareKeys = (a, b) => {
if (a.age < b.age) return -1
if (a.age > b.age) return 1
return 0
}
const bst = new BinarySearchTree({ compareKeys })
bst.insert({ age: 23 }, 'Mark')
bst.insert({ age: 47 }, 'Franck')
Custom value checking
const checkValueEquality = (a, b) => a.length === b.length
var bst = new BinarySearchTree({ checkValueEquality })
bst.insert(10, 'hello')
bst.insert(10, 'world')
bst.insert(10, 'howdoyoudo')
bst.delete(10, 'abcde')
bst.search(10)
License
The package is released under the MIT License as the original package.
See LICENSE.md.